<template>
    <el-dialog v-model="addDialog" title="菜单添加" width="800">
        <el-form
                ref="ruleFormRef"
                :model="formData"
                :rules="rules"
                class="demo-ruleForm"
        >
            <el-form-item label="父级菜单" label-width="120" prop="pid">
                <el-tree-select
                        v-model="formData.pid"
                        :data="menuList"
                        :props="defaultProps"
                        node-key="id"
                        check-strictly
                        :render-after-expand="false"
                        :default-expand-all="true"
                        style="width: 240px"
                />
            </el-form-item>
            <el-form-item label="菜单名称" label-width="120" prop="menuName">
                <el-input autocomplete="off" v-model="formData.menuName" />
            </el-form-item>
            <el-form-item label="菜单描述" label-width="120" prop="menuDesc">
                <el-input type="textarea" autocomplete="off" v-model="formData.menuDesc" />
            </el-form-item>
            <el-form-item label="菜单图标" label-width="120" prop="icon">
                <el-select v-model="formData.icon" placeholder="请选择图标" style="width: 240px">
                    <template #label="{value}">
                        <el-icon>
                            <component :is="value"></component>
                        </el-icon>
                    </template>
                    <el-option
                            v-for="item in icons"
                            :key="item"
                            :label="item"
                            :value="item"
                    >
                        <el-icon>
                            <component :is="item"></component>
                        </el-icon>
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="排序" label-width="120" prop="sortOrder">
                <el-input-number autocomplete="off" v-model="formData.sortOrder" />
            </el-form-item>
            <el-form-item label="权限标识符" label-width="120" prop="permissionCode">
                <el-input autocomplete="off" v-model="formData.permissionCode" />
            </el-form-item>
            <el-form-item label="菜单跳转地址" label-width="120">
                <el-input autocomplete="off" v-model="formData.url" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="addDialog = false">关闭</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                <el-button @click="save(ruleFormRef)" type="primary">保存</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
    import {defineEmits, defineModel, defineProps} from "@vue/runtime-core";
    import { reactive, ref } from 'vue'
    import axios from '@/axios/index.js'
    import {ElMessage} from "element-plus";

    const ruleFormRef = ref()
    let addDialog = defineModel('addDialog')
    const emit = defineEmits(['loadData'])
    let props = defineProps(['tableData'])
    let menuList = [{
        id: '0',
        menuName: '根菜单',
        children: props.tableData
    }]

    const defaultProps = {
        label: 'menuName'
    }

    let formData = reactive({
        menuName: '',
        menuDesc: '',
        icon: '',
        sortOrder: 1,
        permissionCode: '',
        url: '',
        pid: '0',
        status: '1',
    })

    const rules = reactive({
        pid: [{ required: true, message: '请选择菜单父级id', trigger: 'blur' }],
        menuName:[
            { required: true, message: '请输入菜单名称', trigger: 'blur' },
            { min: 2, message: '菜单名不低于2位', trigger: 'blur' },
            ],
        icon:[{required: true, message: '请选择图标', trigger: 'blur' }],
        sortOrder:[{required: true, message: '请选择序号', trigger: 'blur' }],
        permissionCode:[{required: true, message: '请输入权限标识符', trigger: 'blur' }]
    })
    //菜单集合
    let icons = ['plus','minus','circlePlus','search','aim','house','fullScreen','loading','link','service','pointer','star','notification',
        'connection','chatDotRound','setting','clock','position','discount','odometer','chatSquare','chatRound','chatLineRound','chatDotSquare',
        'view','hide','unlock','lock','bell','refresh','muteNotification','user','check','circleCheck','warning','circleClose','close',
        'pieChart','more','compass','filter','switch','editPen','edit','message','messageBox','finished','delete','crop','switchButton',
        'operation','open','remove','zoomOut','zoomIn','download','upload','sort','document','documentAdd','notebook','tickets','memo','collection',
        'postcard','scaleToOriginal','setUp','documentDelete','documentChecked','dataBoard','dataAnalysis','copyDocument','folderChecked','files',
        'folder','folderDelete','folderRemove','folderOpened','documentCopy','documentRemove','folderAdd','reading','dataLine','microphone','mute',
        'mic','videoPause','videoCamera','videoPlay','monitor','film','camera','picture','cellphone','location','locationInformation','deleteLocation',
        'addLocation','mapLocation','place','van','coordinate','medal','goldMedal','help','sell','soldOut','key','coin','shoppingCart','shoppingCartFull',
        'shoppingTrolley','phone','handbag','shoppingBag','trophy','trophyBase','stopwatch','timer','collectionTag','takeawayBox','priceTag','wallet',
        'sunrise','sunny','ship','mostlyCloudy','sunset']
    //保存
    function save(formEl) {
        if (!formEl) return;
        formEl.validate((valid) => {
            if (valid) {
                axios.post("/menu/insert", formData).then((resp) => {
                    ElMessage({
                        message: resp.data.message,
                        type: "success",
                        duration: 1500,
                        onClose: function () {
                            addDialog.value = false;
                            emit("loadData");
                        },
                    });
                });
            } else {
                // 表单验证不通过，进行提示或其他处理
                ElMessage.error("请检查表单填写是否正确");
            }
        });
    }

    const resetForm = (formEl) => {
        if (!formEl) return
        formEl.resetFields()
    }

</script>

<style>

</style>